import { Component, Vue, Inject } from 'vue-property-decorator';

import dayjs from 'dayjs';
import { DATE_TIME_LONG_FORMAT } from '@/shared/date/filters';

import UserAccountService from '@/entities/user-account/user-account.service';
import { IUserAccount } from '@/shared/model/user-account.model';

import ContentInfoService from '@/entities/content-info/content-info.service';
import { IContentInfo } from '@/shared/model/content-info.model';

import { IUserFavorateItem, UserFavorateItem } from '@/shared/model/user-favorate-item.model';
import UserFavorateItemService from './user-favorate-item.service';

const validations: any = {
  userFavorateItem: {
    favorateTime: {},
  },
};

@Component({
  validations,
})
export default class UserFavorateItemUpdate extends Vue {
  @Inject('userFavorateItemService') private userFavorateItemService: () => UserFavorateItemService;
  public userFavorateItem: IUserFavorateItem = new UserFavorateItem();

  @Inject('userAccountService') private userAccountService: () => UserAccountService;

  public userAccounts: IUserAccount[] = [];

  @Inject('contentInfoService') private contentInfoService: () => ContentInfoService;

  public contentInfos: IContentInfo[] = [];
  public isSaving = false;
  public currentLanguage = '';

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.userFavorateItemId) {
        vm.retrieveUserFavorateItem(to.params.userFavorateItemId);
      }
      vm.initRelationships();
    });
  }

  created(): void {
    this.currentLanguage = this.$store.getters.currentLanguage;
    this.$store.watch(
      () => this.$store.getters.currentLanguage,
      () => {
        this.currentLanguage = this.$store.getters.currentLanguage;
      }
    );
  }

  public save(): void {
    this.isSaving = true;
    if (this.userFavorateItem.id) {
      this.userFavorateItemService()
        .update(this.userFavorateItem)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = this.$t('jhipsterserverApp.userFavorateItem.updated', { param: param.id });
          return this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Info',
            variant: 'info',
            solid: true,
            autoHideDelay: 5000,
          });
        });
    } else {
      this.userFavorateItemService()
        .create(this.userFavorateItem)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = this.$t('jhipsterserverApp.userFavorateItem.created', { param: param.id });
          this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Success',
            variant: 'success',
            solid: true,
            autoHideDelay: 5000,
          });
        });
    }
  }

  public convertDateTimeFromServer(date: Date): string {
    if (date && dayjs(date).isValid()) {
      return dayjs(date).format(DATE_TIME_LONG_FORMAT);
    }
    return null;
  }

  public updateInstantField(field, event) {
    if (event.target.value) {
      this.userFavorateItem[field] = dayjs(event.target.value, DATE_TIME_LONG_FORMAT);
    } else {
      this.userFavorateItem[field] = null;
    }
  }

  public updateZonedDateTimeField(field, event) {
    if (event.target.value) {
      this.userFavorateItem[field] = dayjs(event.target.value, DATE_TIME_LONG_FORMAT);
    } else {
      this.userFavorateItem[field] = null;
    }
  }

  public retrieveUserFavorateItem(userFavorateItemId): void {
    this.userFavorateItemService()
      .find(userFavorateItemId)
      .then(res => {
        res.favorateTime = new Date(res.favorateTime);
        this.userFavorateItem = res;
      });
  }

  public previousState(): void {
    this.$router.go(-1);
  }

  public initRelationships(): void {
    this.userAccountService()
      .retrieve()
      .then(res => {
        this.userAccounts = res.data;
      });
    this.contentInfoService()
      .retrieve()
      .then(res => {
        this.contentInfos = res.data;
      });
  }
}
